﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Namespace Backup
    Public Class Jobs
        Shared Sub New()
            Dim aSelectColumns As String() = {"Id", "UniqueId", "Name", "LastRunUtc", "NextRunUtc", "Source", "Destination", "Schedule", "Comment", "Enabled", "EncryptionProperties", "CompressionProperties", "Inertia", "Versioning", "Actions"}
            ObjectPersister.DefineObject(GetType(Entities.Backup.Job), "BackupJobs", aSelectColumns)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "Schedule", AddressOf FieldHandler_Schedule)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "EncryptionProperties", AddressOf FieldHandler_EncryptionProperties)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "CompressionProperties", AddressOf FieldHandler_CompressionProperties)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "Source", AddressOf FieldHandler_Source)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "Destination", AddressOf FieldHandler_Destination)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "Inertia", AddressOf FieldHandler_Inertia)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "Versioning", AddressOf FieldHandler_Versioning)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Backup.Job), "Actions", AddressOf FieldHandler_Actions)
        End Sub
        Private Sub New()
        End Sub
        Private Shared Sub FieldHandler_Source(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.Source = Framework.Serialization.FromXml(Of Entities.Backup.Source)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_Destination(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.Destination = Framework.Serialization.FromXml(Of Entities.Backup.Destination)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_Schedule(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.Schedule = Framework.Serialization.FromXml(Of Entities.Backup.SchduleBase)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_EncryptionProperties(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.EncryptionProperties = Framework.Serialization.FromXml(Of Entities.Backup.EncryptionProperties)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_CompressionProperties(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.CompressionProperties = Framework.Serialization.FromXml(Of Entities.Backup.CompressionProperties)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_Inertia(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.Inertia = Framework.Serialization.FromXml(Of Entities.Backup.Inertia)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_Versioning(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            clsObj.Versioning = Framework.Serialization.FromXml(Of Entities.Backup.Versioning)(CStr(value))
        End Sub
        Private Shared Sub FieldHandler_Actions(ByVal obj As Object, ByVal value As Object)
            Dim clsObj As Entities.Backup.Job = CType(obj, Entities.Backup.Job)
            Dim strVal As String = CStr(value)
            If String.IsNullOrEmpty(strVal) Then
                clsObj.Actions = Nothing
            Else
                clsObj.Actions = Framework.Serialization.FromXml(Of Entities.Backup.Actions)(strVal)
            End If
        End Sub

        Public Shared Function GetAll() As Generic.List(Of Entities.Backup.Job)
            Return ObjectPersister.GetAll(Of Entities.Backup.Job)()
        End Function

        Public Shared Sub Save(ByVal job As Entities.Backup.Job)
            Dim clsCmd As New SaveCommand(Of Integer)("BackupJobs", "Id", job.Id, 0)
            clsCmd.AddParam("UniqueId", job.UniqueId, False)
            clsCmd.AddParam("Enabled", job.Enabled, False)
            clsCmd.AddParam("Name", job.Name, False)
            clsCmd.AddParam("Comment", job.Comment, False)
            clsCmd.AddParam("LastRunUtc", job.LastRunUtc, True)
            clsCmd.AddParam("NextRunUtc", job.NextRunUtc, True)
            clsCmd.AddParam("Source", Framework.Serialization.ToXml(job.Source), False)
            clsCmd.AddParam("Destination", Framework.Serialization.ToXml(job.Destination), False)
            clsCmd.AddParam("Schedule", Framework.Serialization.ToXml(job.Schedule), False)
            clsCmd.AddParam("EncryptionProperties", Framework.Serialization.ToXml(job.EncryptionProperties), False)
            clsCmd.AddParam("CompressionProperties", Framework.Serialization.ToXml(job.CompressionProperties), False)
            clsCmd.AddParam("Inertia", Framework.Serialization.ToXml(job.Inertia), False)
            clsCmd.AddParam("Versioning", Framework.Serialization.ToXml(job.Versioning), False)
            clsCmd.AddParam("Actions", Framework.Serialization.ToXml(job.Actions), False)
            clsCmd.Save()
        End Sub
        Public Shared Sub Delete(ByVal job As Entities.Backup.Job, ByVal transactionScope As TransactionScope)
            Dim strSql As String = "DELETE FROM BackupJobs WHERE Id=@Id"
            Dim sqlPar1 As New SqlCeParameter("@Id", job.Id)
            Environment.ExecuteNonQuery(strSql, transactionScope, sqlPar1)
        End Sub
    End Class
End Namespace